package org.springframework.remoting.jsonrpc;

import org.jabsorb.serializer.AbstractSerializer;
import org.jabsorb.serializer.MarshallException;
import org.jabsorb.serializer.ObjectMatch;
import org.jabsorb.serializer.SerializerState;
import org.jabsorb.serializer.UnmarshallException;

public class ClassSerializer extends AbstractSerializer {

	public ClassSerializer() {
		super();
	}

	public Class[] getSerializableClasses() {
		return new Class[] {};
	}

	public boolean canSerialize(Class clazz, Class jsonClazz) {
		String name = clazz.getName();

		boolean canSerialize = name.equals("java.lang.Class");
		return canSerialize;
	}

	public Class[] getJSONClasses() {
		return new Class[] {};
	}

	public Object marshall(SerializerState state, Object o, Object b)
			throws MarshallException {
		String name = ((Class) b).getName();
		return name;
	}

	public ObjectMatch tryUnmarshall(SerializerState state, Class clazz,
			Object json) throws UnmarshallException {
		return null;
	}

	public Object unmarshall(SerializerState state, Class clazz, Object json)
			throws UnmarshallException {
		String sJson = (String) json;
		Object obj = null;
		try {
			if (int.class.getName().equals(json))
				return int.class;
			else if (double.class.getName().equals(json))
				return double.class;
			else if (float.class.getName().equals(json))
				return float.class;
			else if (char.class.getName().equals(json))
				return char.class;
			else if (long.class.getName().equals(json))
				return long.class;
			else if (short.class.getName().equals(json))
				return short.class;
			obj = Class.forName(sJson);
		} catch (ClassNotFoundException e) {
			throw new UnmarshallException(e.getMessage());
		}
		return obj;
	}

}
